# File mcf.mod 
# Min-cost flow problem with supplies and demands. 
# Graph is defined explicitly. Doesn't have to be complete. 
# It is a complete one. 
#-----------
# demands are negative values, supplies are positive.
# One constraint for either type. 



param n;     # Number of nodes;

# notice the use of within here! 
# ARCS is a set, 'within' is the subseteq operator
# the left operand of 'in' must be an expression that evaluates to 
# string or a number. The left operand of 'within' must be a set. 
set ARCS within {1..n, 1..n};

param supply {1..n};

check: sum {i in 1..n} supply[i] = 0;
### This statement will check that the sum of demands 
## is zero, as we
### expect for the problem to be feasible.

param cost {ARCS};
param u {ARCS};

var x {ARCS} >=0;

minimize total_cost:
    sum { (i,j) in ARCS } cost[i,j]*x[i,j];
subject to balance {i in 1..n}:
sum{ (i,j) in ARCS } x[i,j] - sum { (j,i) in ARCS } x[j,i]   = supply[i];
